package main

import (
	"fmt"
	. "go_data_structures_and_algorithms/tool"
)

type Node struct {
	Value int
	next  *Node
}

func (n *Node) Node(data int) {
	n.Value = data

}

func reverseNodeLinkedList(head *Node) *Node {
	var pre *Node  //保存当前节点的前面一个节点用来当做逆序后当前Node的next的内容
	var next *Node //用来保存没逆序之前的当前节点的下一个节点也就是逆序后的前一个节点
	for head != nil {
		next = head.next //先将head的next保存起来为后面做准备
		head.next = pre  //pre保存的是上一个节点，到下一轮就是他的next

		pre = head  //pre再将当前节点保存
		head = next //
	}
	return pre
}

func reverseDoubleNodeLinkedList[T MyInt](head *DoubleNode[T]) *DoubleNode[T] {
	var pre *DoubleNode[T]
	var next *DoubleNode[T]
	for head != nil { //核心思想就是先记录之后的节点的信息，再调整自身指针朝向，再移动指针到下一步
		next = head.Next
		head.Next = pre
		pre = head
		head.Pre = next
		head = next
	}

	return pre
}

func main() {
	//var n Node
	//var n1 Node
	//var n2 Node
	//n.Value = 1
	//n1.Value = 2
	//n2.Value = 3
	//n.next = &n1
	//n1.next = &n2
	//head := reverseNodeLinkedList(&n)
	//
	//for head != nil {
	//	fmt.Println(head.Value)
	//	head = head.next
	//
	//}

	var n DoubleNode[int]
	var n1 DoubleNode[int]
	var n2 DoubleNode[int]
	n.Value = 1
	n1.Value = 2
	n2.Value = 3
	n.Next = &n1
	n1.Next = &n2
	n2.Pre = &n1
	n1.Pre = &n
	head := reverseDoubleNodeLinkedList(&n)

	for head != nil {
		fmt.Println(head.Value)
		head = head.Next

	}
}
